package com.dhcc.DRGsHosp.application.impl;

import java.util.List;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.ExpectedApplication;
import com.dhcc.DRGsHosp.core.domain.Expected;

@Named
@Transactional
public class ExpectedApplicationImpl implements ExpectedApplication {
	
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    
	public Expected getExpected(Double expectedCode) {
		return Expected.get(Expected.class, expectedCode);
	}
	
	public void creatExpected(Expected expected) {
		expected.save();
	}
	
	public void updateExpected(Expected expected) {
		expected .save();
	}
	
	public void removeExpected(Expected expected) {
		if(expected != null){
			expected.remove();
		}
	}
	
	public void removeExpecteds(Set<Expected> expecteds) {
		for (Expected expected : expecteds) {
			expected.remove();
		}
	}
	
	public List<Expected> findAllExpected() {
		return Expected.findAll(Expected.class);
	}

	@Override
	public List<Expected> getExpected(String type1,String year, String type) {
		StringBuilder sql = new StringBuilder(" select _expected from Expected _expected ");
		sql.append(" where _expected.expectedCode like '%"+year+"%' "
				+ "and _expected.expectedType = '"+type1+"'" );
		if("depGroup".equals(type)){
			sql.append(" and _expected.parDepGroupCode<>'DRG_DHCC' ");
		}else{
			sql.append(" and _expected.parDepGroupCode='DRG_DHCC' ");
		}
		if("dep".equals(type)){
			sql.append(" and _expected.parDepCode<>'DRG_DHCC' ");
		}else{
			sql.append(" and _expected.parDepCode='DRG_DHCC' ");
		}
		if("mdc".equals(type)){
			sql.append(" and _expected.parMDCCode<>'DRG_DHCC' ");
		}else{
			sql.append(" and _expected.parMDCCode='DRG_DHCC' ");
		}
		if("adrg".equals(type)){
			sql.append(" and _expected.parADRGCode<>'DRG_DHCC' ");
		}else{
			sql.append(" and _expected.parADRGCode='DRG_DHCC' ");
		}
		if("drg".equals(type)){
			sql.append(" and _expected.pardrgCode<>'DRG_DHCC' ");
		}else{
			sql.append(" and _expected.pardrgCode='DRG_DHCC' ");
		}
		List<Expected> resultList = getQueryChannelService().createJpqlQuery(sql.toString()).list();
		return resultList;
	}

}
